// pages/register/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    realName: '',
    code: null,
    member_info: {
      iv: null,
      encryptedData: null
    },
    mobile_info: {
      iv: null,
      encryptedData: null
    },
    tried_key: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tried_key:app.globalData.tried_key
    })
    console.log(this.data.tried_key)
  },

  // 获取真实姓名(待优化--延迟数据绑定)
  inputEdit: function (e) {
    // console.log(e)
    let inputModel = e.currentTarget.dataset.name;
    let value = e.detail.value;
    this.data[inputModel] = value;
    this.setData({
      realName: this.data[inputModel]
    });
    console.log(this.data.realName)
  },

  // 第一步获取用户授权
  getUserProfile: function () {
    wx.getUserProfile({
      desc: "获取用户授权",
      success: res => {
        // console.log(res)
        this.data.member_info.encryptedData = res.encryptedData
        this.data.member_info.iv = res.iv
        this.setData({
          step: 2
        })
      }
    })
  },

  // 第二步获取用户手机号
  getPhoneNumber: function (e) {
    // console.log(e)
    this.data.mobile_info.encryptedData = e.detail.encryptedData
    this.data.mobile_info.iv = e.detail.iv
    if (e.detail.encryptedData && e.detail.iv) {
      wx.login({
        timeout: 1000,
        success: res => {
          if (res.code) {
            // console.log(this)
            this.data.code = res.code
            this.setData({
              step: 3
            })
          }
        }
      })
    }
  },

  // 提交注册
  submit: function () {
    wx.request({
      url: 'https://c2.lusuokeji.com/modules/core/platform/1/app/wechat/miniapp/reg',
      method: 'POST',
      data: {
        code: this.data.code,
        third_key: this.data.tried_key,
        member_info: this.data.member_info,
        realname: this.data.realName,
        mobile_info: this.data.mobile_info
      },
      success: res => {
        console.log(res)
        if (res.statusCode === 200) {
          wx.showToast({
            title: '注册成功',
            icon:'none',
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '../index/index',
            })
          }, 1000);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})